﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tilver.WCF.Client.CalculatorService;
using System.ServiceModel;
using Tilver.WCF.Contracts;

namespace Tilver.WCF.Client
{
    class Program
    {
        static void Main(string[] args)
        {
            //有Service References引用
            /*
            using (CalculatorServiceClient client = new CalculatorServiceClient())
            {
                double x = 1, y = 2;
                Console.WriteLine("x+y={2} when x={0},y={1}", x, y, client.Add(x, y));
                Console.WriteLine("x-y={2} when x={0},y={1}", x, y, client.Substract(x, y));
                Console.WriteLine("x*y={2} when x={0},y={1}", x, y, client.Multiply(x, y));
                Console.WriteLine("x/y={2} when x={0},y={1}", x, y, client.Divide(x, y));
                Console.Read();
            }
            */
            //通过ChannelFactory<T>调用WCF服务
            /*
            using (ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:9000/calculatorservice"))
            {
                ICalculator client = factory.CreateChannel();
                using (client as IDisposable)
                {
                    double x = 1, y = 2;
                    Console.WriteLine("x+y={2} when x={0},y={1}", x, y, client.Add(x, y));
                    Console.WriteLine("x-y={2} when x={0},y={1}", x, y, client.Substract(x, y));
                    Console.WriteLine("x*y={2} when x={0},y={1}", x, y, client.Multiply(x, y));
                    Console.WriteLine("x/y={2} when x={0},y={1}", x, y, client.Divide(x, y));
                }
                Console.Read();
            }
            */
            //通用配置文件
            
            using (ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>("calculatorservice"))
            {
                ICalculator client = factory.CreateChannel();
                using (client as IDisposable)
                {
                    double x = 1, y = 2;
                    Console.WriteLine("x+y={2} when x={0},y={1}", x, y, client.Add(x, y));
                    Console.WriteLine("x-y={2} when x={0},y={1}", x, y, client.Substract(x, y));
                    Console.WriteLine("x*y={2} when x={0},y={1}", x, y, client.Multiply(x, y));
                    Console.WriteLine("x/y={2} when x={0},y={1}", x, y, client.Divide(x, y));
                }
                Console.Read();
            }
            
        }
    }
}
